package com.cw.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.cw.data.ConstantEnum;
import com.cw.data.PageBean;
import com.cw.data.Result;
import com.cw.pojo.TbBrand;
import com.cw.pojo.TbSpecification;
import com.cw.pojo.TbTypeTemplate;
import com.cw.service.BrandService;
import com.cw.service.ItemService;
import com.cw.service.TemplateService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("templateController")
public class TemplateController {

    @Reference
    private TemplateService templateService;

    @RequestMapping("toTemplate")
    public String toTemplate(Model model) {

        List<TbBrand> brands = templateService.getBrandList();

        model.addAttribute("brandList", brands);

        List<TbSpecification> specList = templateService.getSpecList();

        model.addAttribute("specList", specList);

        return "admin/type_template.html";
    }

    @RequestMapping("getTemplateInfo")
    @ResponseBody
    public PageBean<TbTypeTemplate> getTemplateInfo(@RequestParam(defaultValue = "1") Integer pageNumber, @RequestParam(defaultValue = "5") Integer pageSize, String name) {

        try {

            PageBean<TbTypeTemplate> pa = templateService.getTemplateInfo(pageNumber, pageSize, name);

            return pa;

        }catch (Exception e) {

            e.printStackTrace();

            return null;
        }
    }

    @RequestMapping("addTemplate")
    @ResponseBody
    public Result addTemplate(TbTypeTemplate tt) {

        try {

            templateService.addTemplate(tt);

            return Result.result(ConstantEnum.SUCCESS);

        }catch (Exception e) {

            e.printStackTrace();

            return Result.result(ConstantEnum.ERROR);
        }
    }

    @RequestMapping("getOneTemplate")
    @ResponseBody
    public Result getOneTemplate(Long id) {

        try {

            TbTypeTemplate tt = templateService.getOneTemplate(id);

            return Result.result(ConstantEnum.SUCCESS).setData(tt);

        }catch (Exception e) {

            e.printStackTrace();

            return Result.result(ConstantEnum.ERROR);
        }
    }

    @RequestMapping("updateTemplate")
    @ResponseBody
    public Result updateTemplate(TbTypeTemplate tt) {

        try {

            templateService.updateTemplate(tt);

            return Result.result(ConstantEnum.SUCCESS);
        }catch (Exception e) {

            e.printStackTrace();

            return Result.result(ConstantEnum.ERROR);
        }
    }

    @RequestMapping("deleteTemplate")
    @ResponseBody
    public Result deleteTemplate(Long id) {

        try {

            templateService.deleteTemplate(id);

            return Result.result(ConstantEnum.SUCCESS);
        }catch (Exception e) {

            e.printStackTrace();

            return Result.result(ConstantEnum.ERROR);
        }
    }

    @RequestMapping("deleteAll")
    @ResponseBody
    public Result deleteAll(Long[] ids) {

        try {

            templateService.deleteAll(ids);

            return Result.result(ConstantEnum.SUCCESS);
        }catch (Exception e) {

            e.printStackTrace();

            return Result.result(ConstantEnum.ERROR);
        }

    }
}
